#include "stm32f10x.h"                  // Device header
#include "TFT.h" 
//#include "Font.h" 
#include "Calculate.h" 


//液晶IO初始化配置
void TFT_SPI_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//开启系统时钟2
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//开启AFIO准备进行引脚复用
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//解除GPIOA Pin15的调试复用
	
	GPIO_InitTypeDef GPIO_B_InitStructure; //结构体
	GPIO_B_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//配置为推挽输出
	GPIO_B_InitStructure.GPIO_Pin=GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;//配置为4,5,7,8号引脚
	GPIO_B_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//速度为50Hz
	GPIO_Init(GPIOB,&GPIO_B_InitStructure);//初始化GPIOB
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//开启系统时钟2
	
	GPIO_InitTypeDef GPIO_A_InitStructure; //结构体
	GPIO_A_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//配置为推挽输出
	GPIO_A_InitStructure.GPIO_Pin=GPIO_Pin_15;//配置为15号引脚
	GPIO_A_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//速度为50Hz
	GPIO_Init(GPIOA,&GPIO_A_InitStructure);//初始化GPIOA
	
}


//向SPI总线传输一个8位数据
void  SPI_WriteData(unsigned char Data)
{
	unsigned char i=0;
	for(i=8;i>0;i--)
	{
		if(Data&0x80)	
			LCD_SDA_SET; //输出数据
		else LCD_SDA_CLR;
		
		LCD_SCL_CLR;       
		LCD_SCL_SET;
		Data<<=1; 
	}
}

//向液晶屏写一个8位指令
void Lcd_WriteIndex(unsigned char Index)
{
	//SPI 写命令时序开始
	LCD_CS_CLR;
	LCD_RS_CLR;
	SPI_WriteData(Index);
	LCD_CS_SET;
}

//向液晶屏写一个8位数据
void Lcd_WriteData(unsigned char Data)
{
	LCD_CS_CLR;
	LCD_RS_SET;
	SPI_WriteData(Data);
	LCD_CS_SET; 
}

//向液晶屏写一个16位数据
void LCD_WriteData_16Bit(unsigned int Data)
{
	LCD_CS_CLR;
	LCD_RS_SET;
	SPI_WriteData(Data>>8); 	//写入高8位数据
	SPI_WriteData(Data); 			//写入低8位数据
	LCD_CS_SET; 
}

void Lcd_WriteReg(unsigned char Index,unsigned char Data)
{
	Lcd_WriteIndex(Index);
	Lcd_WriteData(Data);
}

void Lcd_Reset(void)
{
	unsigned int i;
	LCD_RST_CLR;
	//delay_ms(100);
	for(i=0;i<6000;i++);
	LCD_RST_SET;
	for(i=0;i<6000;i++);
}
void Lcd_Init(void)
{	
	unsigned int i;
	
	TFT_SPI_Init();
	Lcd_Reset(); //Reset before LCD Init.
	
	//LCD Init For 1.44Inch LCD Panel with ST7735R.
	Lcd_WriteIndex(0x11);//Sleep exit 
	//delay_ms (120);
	
	for(i=0;i<65000;i++);
	for(i=0;i<65000;i++);
	
	//ST7735R Frame Rate
	Lcd_WriteIndex(0xB1); 
	Lcd_WriteData(0x01); 
	Lcd_WriteData(0x2C); 
	Lcd_WriteData(0x2D); 
	
	Lcd_WriteIndex(0xB2); 
	Lcd_WriteData(0x01); 
	Lcd_WriteData(0x2C); 
	Lcd_WriteData(0x2D); 
	
	Lcd_WriteIndex(0xB3); 
	Lcd_WriteData(0x01); 
	Lcd_WriteData(0x2C); 
	Lcd_WriteData(0x2D); 
	Lcd_WriteData(0x01); 
	Lcd_WriteData(0x2C); 
	Lcd_WriteData(0x2D); 
	
	Lcd_WriteIndex(0xB4); //Column inversion 
	Lcd_WriteData(0x07); 
	
	//ST7735R Power Sequence
	Lcd_WriteIndex(0xC0); 
	Lcd_WriteData(0xA2); 
	Lcd_WriteData(0x02); 
	Lcd_WriteData(0x84); 
	Lcd_WriteIndex(0xC1); 
	Lcd_WriteData(0xC5); 
	
	Lcd_WriteIndex(0xC2); 
	Lcd_WriteData(0x0A); 
	Lcd_WriteData(0x00); 
	
	Lcd_WriteIndex(0xC3); 
	Lcd_WriteData(0x8A); 
	Lcd_WriteData(0x2A); 
	Lcd_WriteIndex(0xC4); 
	Lcd_WriteData(0x8A); 
	Lcd_WriteData(0xEE); 
	
	Lcd_WriteIndex(0xC5); //VCOM 
	Lcd_WriteData(0x0E); 
	
	Lcd_WriteIndex(0x36); //MX, MY, RGB mode 
	Lcd_WriteData(0xC0); 
	
	//ST7735R Gamma Sequence
	Lcd_WriteIndex(0xe0); 
	Lcd_WriteData(0x0f); 
	Lcd_WriteData(0x1a); 
	Lcd_WriteData(0x0f); 
	Lcd_WriteData(0x18); 
	Lcd_WriteData(0x2f); 
	Lcd_WriteData(0x28); 
	Lcd_WriteData(0x20); 
	Lcd_WriteData(0x22); 
	Lcd_WriteData(0x1f); 
	Lcd_WriteData(0x1b); 
	Lcd_WriteData(0x23); 
	Lcd_WriteData(0x37); 
	Lcd_WriteData(0x00); 	
	Lcd_WriteData(0x07); 
	Lcd_WriteData(0x02); 
	Lcd_WriteData(0x10); 
	
	Lcd_WriteIndex(0xe1); 
	Lcd_WriteData(0x0f); 
	Lcd_WriteData(0x1b); 
	Lcd_WriteData(0x0f); 
	Lcd_WriteData(0x17); 
	Lcd_WriteData(0x33); 
	Lcd_WriteData(0x2c); 
	Lcd_WriteData(0x29); 
	Lcd_WriteData(0x2e); 
	Lcd_WriteData(0x30); 
	Lcd_WriteData(0x30); 
	Lcd_WriteData(0x39); 
	Lcd_WriteData(0x3f); 
	Lcd_WriteData(0x00); 
	Lcd_WriteData(0x07); 
	Lcd_WriteData(0x03); 
	Lcd_WriteData(0x10);  
	
	Lcd_WriteIndex(0x2a);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x7f);
	
	Lcd_WriteIndex(0x2b);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x00);
	Lcd_WriteData(0x9f);
	
	Lcd_WriteIndex(0xF0); //Enable test command  
	Lcd_WriteData(0x01); 
	Lcd_WriteIndex(0xF6); //Disable ram power save mode 
	Lcd_WriteData(0x00); 
	
	Lcd_WriteIndex(0x3A); //65k mode 
	Lcd_WriteData(0x05); 
	
	
	Lcd_WriteIndex(0x29);//Display on	 
}



/*************************************************
  函数名：LCD_Set_Region
  功能：设置lcd显示区域，在此区域写点数据自动换行
  入口参数：xy起点和终点
  返回值：无
 *************************************************/
void Lcd_SetRegion(unsigned int x_start,unsigned int y_start,unsigned int x_end,unsigned int y_end)
{		
	Lcd_WriteIndex(0x2a);
	Lcd_WriteData(0x00);
	Lcd_WriteData(x_start);//Lcd_WriteData(x_start+2);
	Lcd_WriteData(0x00);
	Lcd_WriteData(x_end+2);
	
	Lcd_WriteIndex(0x2b);
	Lcd_WriteData(0x00);
	Lcd_WriteData(y_start+0);
	Lcd_WriteData(0x00);
	Lcd_WriteData(y_end+1);
	
	Lcd_WriteIndex(0x2c);
	
}

/*************************************************
  函数名：LCD_Set_XY
  功能：设置lcd显示起始点
  入口参数：xy坐标
  返回值：无
 *************************************************/
void Lcd_SetXY(unsigned int x,unsigned int y)
{
	Lcd_SetRegion(x,y,x,y);
}


/*************************************************
  函数名：LCD_DrawPoint
  功能：画一个点
  入口参数：无
  返回值：无
 *************************************************/
void Gui_DrawPoint(unsigned int x,unsigned int y,unsigned int Data)
{
	Lcd_SetRegion(x,y,x+1,y+1);
	LCD_WriteData_16Bit(Data);
	
}    

/*****************************************
  函数功能：读TFT某一点的颜色                          
  出口参数：color  点颜色值                                 
 ******************************************/
unsigned int Lcd_ReadPoint(unsigned int x,unsigned int y)
{
	unsigned int Data;
	Lcd_SetXY(x,y);
	Lcd_WriteData(Data);
	return Data;
}
/*************************************************
  函数名：Lcd_Clear
  功能：全屏清屏函数
  入口参数：填充颜色COLOR
  返回值：无
 *************************************************/
void Lcd_Clear(unsigned int Color)     //刷新全屏           
{	
	unsigned int i,m;
	Lcd_SetRegion(0,0,X_MAX_PIXEL-1,Y_MAX_PIXEL-1);
	Lcd_WriteIndex(0x2C);
	for(i=0;i<X_MAX_PIXEL;i++)
		for(m=0;m<Y_MAX_PIXEL;m++)
		{	
			LCD_WriteData_16Bit(Color);
		}   
}

void Lcd_Clear_1(unsigned int Color)      //刷新局部           
{	
	unsigned int i,m;
	Lcd_SetRegion(105,137,120,152-1);
	Lcd_WriteIndex(0x2C);
	for(i=0;i<120;i++)
		for(m=0;m<152;m++)
		{	
			LCD_WriteData_16Bit(Color);
		}  
}

/*
屏幕显示测试
*/

void Redraw_Mainmenu(void)
{
	Lcd_Clear(GRAY0);
	Gui_DrawFont_GBK16(15,5,BLUE,GRAY0,"实验平台");
	
	
	//值的边框
	DisplayButtonUp(85,27,125,45); //x1,y1,x2,y2  //TO
	DisplayButtonUp(85,49,125,67); //x1,y1,x2,y2  //ONE
}



void Num_Test(void)
{
//	 char txt[32];
//	 char txt1[32];
//	
//		sprintf(txt, "%d", tempdata);
//		Gui_DrawFont_GBK16(90,25,BLUE,GRAY0,txt);	
//			
//		sprintf(txt1, "%d", humidata);
//		Gui_DrawFont_GBK16(90,47,RED,GRAY0,txt1);
}


void Color_Test(void)
{
	unsigned int i=1;
	Lcd_Clear(GRAY0);
	
	Gui_DrawFont_GBK16(20,10,BLUE,GRAY0,"Color Test");
	
	while(i--)
	{
		Lcd_Clear(WHITE);
		Lcd_Clear(BLACK);
		Lcd_Clear(RED);
		Lcd_Clear(GREEN);
		Lcd_Clear(BLUE);
	}		
}

//取模方式 水平扫描 从左到右 低位在前
void showimage(const unsigned char *p) //显示40*40 QQ图片
{
	int i,j,k; 
	unsigned char picH,picL;
	Lcd_Clear(WHITE); //清屏  
	
	for(k=0;k<4;k++)
	{
		for(j=0;j<3;j++)
		{	
			Lcd_SetRegion(40*j+2,40*k,40*j+39,40*k+39);		//坐标设置
			for(i=0;i<40*40;i++)
			{	
				picL=*(p+i*2);	//数据低位在前
				picH=*(p+i*2+1);				
				LCD_WriteData_16Bit(picH<<8|picL);  						
			}	
		}
	}		
}

void QDTFT_Test_Demo(void)
{
	// LCD_LED_SET;//通过IO控制背光亮				
	Num_Test();//	
}


